package xing.test.voice.local;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.IOException;

import javax.sound.sampled.AudioFileFormat;
import javax.sound.sampled.AudioFormat;
import javax.sound.sampled.AudioInputStream;
import javax.sound.sampled.AudioSystem;
import javax.sound.sampled.DataLine;
import javax.sound.sampled.LineUnavailableException;
import javax.sound.sampled.TargetDataLine;

public class Record2File implements Runnable {
	TargetDataLine line;

	Thread thread;

	public void start() {
		thread = new Thread(this);
		thread.setName("Capture");
		thread.start();
	}

	public void stop() {
		thread = null;
	}

	private void shutDown(String message) {
		thread = null;
	}

	public void run() {
		AudioInputStream audioInputStream = null;
		// get an AudioInputStream of the desired format for playback
		AudioFormat.Encoding encoding = AudioFormat.Encoding.PCM_SIGNED;
		float rate = 8000f;
		int sampleSize = 8;
		boolean bigEndian = true;
		int channels = 1;
		AudioFormat format = new AudioFormat(encoding, rate, sampleSize,
				channels, (sampleSize / 8) * channels, rate, bigEndian);
		DataLine.Info info = new DataLine.Info(TargetDataLine.class, format);
		TargetDataLine line;
		if (!AudioSystem.isLineSupported(info)) {
			return;
		}
		// get an AudioInputStream of the desired format for playback
		try {
			line = (TargetDataLine) AudioSystem.getLine(info);
			line.open(format, line.getBufferSize());
		} catch (Exception ex) {
			ex.printStackTrace();
			return;
		}
		// play back the captured audio data
		ByteArrayOutputStream out = new ByteArrayOutputStream();
		int frameSizeInBytes = format.getFrameSize();
		int bufferLengthInFrames = line.getBufferSize() / 8;
		int bufferLengthInBytes = bufferLengthInFrames * frameSizeInBytes;
		byte[] data = new byte[bufferLengthInBytes];
		int numBytesRead;
		line.start();
		while (thread != null) {
			if ((numBytesRead = line.read(data, 0, bufferLengthInBytes)) == -1) {
				break;
			}
			out.write(data, 0, numBytesRead);
		}
		// we reached the end of the stream. stop and close the line.
		line.stop();
		line.close();
		line = null;
		// stop and close the output stream
		try {
			out.flush();
			out.close();
		} catch (IOException ex) {
			ex.printStackTrace();
		}
		// load bytes into the audio input stream for playback
		byte audioBytes[] = out.toByteArray();
		ByteArrayInputStream bais = new ByteArrayInputStream(audioBytes);
		audioInputStream = new AudioInputStream(bais, format, audioBytes.length
				/ frameSizeInBytes);
		long milliseconds = (long) ((audioInputStream.getFrameLength() * 1000) / format
				.getFrameRate());
		double duration = milliseconds / 1000.0;
		try {
			audioInputStream.reset();
		} catch (Exception ex) {
			ex.printStackTrace();
			return;
		}
		// saveToFile("/home/xing/Public/voice/record.wav",
		// AudioFileFormat.Type.WAVE);
		System.out.println("开始写入");
		File file = new File("/home/xing/Public/voice/record.wav");
		try {
			if (AudioSystem.write(audioInputStream, AudioFileFormat.Type.WAVE,
					file) == -1) {
				throw new IOException("Problems writing to file");
			}
		} catch (Exception ex) {
			ex.printStackTrace();
		}
	}

	public static void main(String[] args) throws Exception {
		Record2File re = new Record2File();
		re.start();
		Thread.sleep(5000);
		re.stop();
	}
}
